package xyz.ljcc.ccmt.service.impl.records;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import xyz.ljcc.ccmt.commons.utils.layui.LayUiFixed;
import xyz.ljcc.ccmt.commons.utils.layui.LayuiColumn;
import xyz.ljcc.ccmt.model.base.DocsReportColumnVsPat;
import xyz.ljcc.ccmt.model.base.DocsReportName;
import xyz.ljcc.ccmt.model.base.DocsReportNameColumnConfig;
import xyz.ljcc.ccmt.repository.admin.DocsReportColumnVsPatRepository;
import xyz.ljcc.ccmt.repository.admin.DocsReportNameColumnConfigRepository;
import xyz.ljcc.ccmt.repository.admin.DocsReportNameRepository;
import xyz.ljcc.ccmt.service.api.records.BaseRecordService;

@Service
public class BaseRecordServiceImpl implements BaseRecordService {

	@Autowired
	private DocsReportNameRepository docsReportNameRepository;
	
	@Autowired
	private DocsReportNameColumnConfigRepository docsReportNameColumnConfigRepository;
	
	@Autowired
	private DocsReportColumnVsPatRepository docsReportColumnVsPatRepository;
	
	@Override
	public Object DoQueryReport(HttpServletRequest request) {
		
		
		
		return null;
	}

	@Override
	public Object DoSaveReport() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<List<LayuiColumn>> loadLayColumns(String reportName,String patientId,String visitId) {
		List<List<LayuiColumn>> result = new ArrayList<>();
		// 1、表单对应的主表
		DocsReportName docsReportName = docsReportNameRepository.findByReportName(reportName);
		// 2、表单对应的列字段集合
		List<DocsReportNameColumnConfig> reportNameCols = docsReportNameColumnConfigRepository.findByReportIdOrderBySort(docsReportName.getId());
		int reportNameFloor = docsReportName.getFloor();  // 表单总层数
		
		for(int ii = 1;ii <= reportNameFloor ;ii++) {
			int floor = ii;
			List<LayuiColumn> lastList = new ArrayList<>();
			List<DocsReportNameColumnConfig> list = reportNameCols.stream().
					filter((DocsReportNameColumnConfig col) -> col.getFloor().equals(floor))
					.collect(Collectors.toList());
			for (DocsReportNameColumnConfig docsReportNameColumnConfig : list) {
				LayuiColumn column = new LayuiColumn();
				column.setField(docsReportNameColumnConfig.getField()); // 字段名
				column.setTitle(docsReportNameColumnConfig.getTitle()); // 显示名
				column.setRowspan(docsReportNameColumnConfig.getRowSpan());
				column.setColspan(docsReportNameColumnConfig.getColSpan()); // getColumnCount(reportNameCols,docsReportNameColumnConfig)
				column.setWidth(docsReportNameColumnConfig.getWidth());
				if(!StringUtils.isEmpty(docsReportNameColumnConfig.getField()) && docsReportNameColumnConfig.getField().startsWith("自定义")) {
					 // 设置自定义列
					DocsReportColumnVsPat patCol = docsReportColumnVsPatRepository.findByColIdAndPatientIdAndVisitId(docsReportNameColumnConfig.getId(), patientId, visitId);
					if(patCol != null) {
						column.setTitle(patCol.getLastShowTtile()); 
						column.setField(patCol.getLastField());
					}
				}
				// 设置列隐藏
				if("USER_ID".equals(docsReportNameColumnConfig.getField()) || "ID".equals(docsReportNameColumnConfig.getField())) {
					column.setStyle("display:none");
				}
				if(docsReportNameColumnConfig.getSave() == 1) { // 保存列字段
					column.setEdit("text");
				}
				if("日期时间".equals(docsReportNameColumnConfig.getField()) || "日期".equals(docsReportNameColumnConfig.getField()) || "时间".equals(docsReportNameColumnConfig.getField())) {
					column.setFixed(LayUiFixed.LEFT.getValue());
				}
				lastList.add(column);
			}			
			result.add(lastList);
		}
		return result;
	}
	
	/**
	 * 
	 * @param reportNameCols
	 * @param col
	 * @return
	 */
	@SuppressWarnings("unused")
	private int getColumnCount(List<DocsReportNameColumnConfig> reportNameCols,DocsReportNameColumnConfig col) {
		int count = 0;
		List<DocsReportNameColumnConfig> list = 
				reportNameCols.stream()
							.filter((DocsReportNameColumnConfig col2) -> col2.getpId() == col.getId())
							.collect(Collectors.toList());
		if(list.size() > 0) {
			for (DocsReportNameColumnConfig docsReportNameColumnConfig : list) {
				count += getColumnCount(reportNameCols,docsReportNameColumnConfig);
			}
		}else {
			count++;
		}
		return count;
	}
	
}
